package com.bwie.gddproject.regist.preccenter;

import android.text.TextUtils;
import android.util.Log;

import com.bwie.gddproject.index.model.bean.BaseBean;
import com.bwie.gddproject.net.OnNetLitenner;
import com.bwie.gddproject.regist.model.IRegistModel;
import com.bwie.gddproject.regist.model.RegistModel;
import com.bwie.gddproject.regist.view.IRegistActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 我走路带风 on 2017/11/9.
 */

public class RegistPresenter {
    private IRegistActivity registActivity;
    private IRegistModel registModel;

    public RegistPresenter(IRegistActivity registActivity) {
        this.registActivity = registActivity;
        registModel = new RegistModel();
    }


    public void regist(){
        if (checkAccount(registActivity.getAccount())&&checkPwd(registActivity.getPwd()))
        {
            registModel.regist(registActivity.getAccount(), registActivity.getPwd(), new OnNetLitenner<BaseBean>() {
                        @Override
                        public void success(BaseBean baseBean) {
                            if (baseBean.getCode().equals("1")){
                                Log.e("tag", "success: "+baseBean.getMsg() );
                                registActivity.show(baseBean.getMsg());
                                registActivity.finishThis();
                            }else{
                                registActivity.show(baseBean.getMsg());
                            }

                        }

                        @Override
                        public void fail(String errer) {

                        }
                    }
            );
        }


    }


    private boolean checkPwd(String pwd) {
        if (TextUtils.isEmpty(pwd)) {
            //给用户提示，输入的账号不能为空
            registActivity.show("请输入密码");
            return false;
        }
        if (pwd.length() != 6) {
            registActivity.show("请输入6位密码");
            return false;
        }
        return true;
    }
    /*
    判断是否是手机号
     */
    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    private boolean checkAccount(String account){

        if (TextUtils.isEmpty(account)){
            registActivity.show("请输入账号");
            return false;
        }

        if (!isMobileNO(account)){
            registActivity.show("请输入正确的手机号");
            return false;
        }

        return true;

    }
}
